package com.kdtech.analyse.video;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 威海传媒网
 * @author Persh
 */
public class WeihaiAnalyse implements AnalyseNews{



	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://v.weihai.tv/play_.*.html",
				"http://v.weihai.tv/play.html[?]c=.*"
				};
		
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		video.setUrl(url);		
		
//		String title = null;
//		String desc = null;
//		Long date = null;
//		String tempdate = null;
//		Document doc = Jsoup.parse(htmltxt);
		
		/**
		 * 解析更新路径  http://v.weihai.tv/ajax.aspx?action=get_player&c=0|vl15yj60bxj304m1&rnd=823717914
		 */
		String idStr = null;
		if(url.contains("?c=")){
			idStr = url.substring(url.indexOf("=")+1);
			
		}else{
			String[] split = url.split("/");
			idStr = split[split.length-1];
			idStr = idStr.substring(idStr.indexOf("_")+1, idStr.indexOf("."));
		}
		if(idStr==null){
		}
		String updateUrl = "http://v.weihai.tv/ajax.aspx?action=get_player&c=0|"+idStr+"&rnd=823717914";
		video.setUpdateUrl(updateUrl);
		NewsMeta update = Update(video);
//		title = doc.select("div#ov_play_title").text();
//		if(title==null || title.trim().length()==0){
//		}
//		desc = doc.select("div.news-main>div.news-content>p").text();
//		tempdate = doc.select("html body div#Container div#PageBody div.VSideBar div.userInfo ul.user li.l2 span#ov_play_addtime").text();
//		if(tempdate==null || tempdate.trim().length()==0){
//		}
//		date = DateUtils.matchDate(tempdate);
//		video.setTitle(StringUtils.trimSpace(title));
//		video.setDesc(desc);
//		video.setDate(date);
		
		return update;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html=responseToURL.getHtml();
					String title = html.substring(html.indexOf("innerHTML=")+"innerHTML=".length(), html.indexOf(";}"));
					if(title!=null && title.trim().length()!=0){
						title = title.replace("\"", "");
						meta.setTitle(title);
					}else{
						return null;
					}
					String tempdate = html.substring(html.indexOf("ov_play_addtime').innerHTML=")+"ov_play_addtime').innerHTML=".length());
					tempdate = tempdate.substring(0, tempdate.indexOf(";}"));
					tempdate = tempdate.replace("\"", "");
					Long date = DateUtils.matchDate(tempdate);
					meta.setDate(date);
					
					String clickStr = html.substring(html.indexOf("ov_play_playcount').innerHTML=")+"ov_play_playcount').innerHTML=".length());
					clickStr = clickStr.substring(0, clickStr.indexOf(";}"));
					clickStr = clickStr.replace("\"", "");
					Integer clickNum = 0;
					clickNum = Integer.parseInt(clickStr);
					meta.setClickNum(clickNum);
					return meta;
				}
			}
		}	
		return null;
	}

	
}
